package chan.module.project.database.tongdxp.hive2.jdbc.test;

import chan.module.project.comm.PropertiesUtil;
import org.apache.commons.text.StringEscapeUtils;

import java.sql.*;
import java.util.Properties;

public class Hive2LocalTestMain {

    // 数据库连接信息
    private static final String URL = "jdbc:hive2://10.10.93.63:10000/default";
    private static final String USER = "";
    private static final String PASSWORD = "";
    private static final String COLLECTION_DELIM = "colelction.delim";
    private static final String FIELD_DELIM = "field.delim";


    public static void main(String[] args) {
        PropertiesUtil util = new PropertiesUtil("F:\\develop\\project\\ChanModuleProject\\module-database\\database-hive2-jdbc-test\\src\\main\\resources\\hive2.properties");
        Properties prop = util.getProp();
        System.out.println(prop.size());
        String a = "\u0001";
    }


    public static void main1(String[] args) {
        Connection conn = null;
        try {
            // 1. 加载驱动
            Class.forName("org.apache.hive.jdbc.HiveDriver");

            // 2. 建立连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            // 3. 增删改查操作
            // 插入数据
            //insert(conn, "张三", 25);

            // 查询数据
            query(conn);

            // 更新数据
            //update(conn, 1, "李四", 30);

            // 删除数据
            //delete(conn, 1);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 4. 关闭连接
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 插入方法
    public static void insert(Connection conn, String name, int age) throws SQLException {
        String sql = "INSERT INTO users(name, age) VALUES(?, ?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, name);
        pstmt.setInt(2, age);
        int count = pstmt.executeUpdate();
        System.out.println("插入了 " + count + " 条记录");
        pstmt.close();
    }

    // 查询方法
    public static void query(Connection conn) throws SQLException {
        String collection_delim = "";
        String field_delim = "";
        String comment = "";
        String sql = "DESCRIBE FORMATTED test_simple_field_12345";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            String data_type = rs.getString(2);
            if(data_type != null ){
                if(data_type.contains(COLLECTION_DELIM)){
                    comment  = rs.getString(3);
                    if(comment != null){
                        collection_delim = comment;
                    }
                }
                if(data_type.contains(FIELD_DELIM)){
                    comment  = rs.getString(3);
                    if(comment != null){
                        field_delim = comment;
                    }
                }
            }
        }
        rs.close();
        stmt.close();
        collection_delim = collection_delim.replaceAll(" ","");
        collection_delim = StringEscapeUtils.unescapeJava(collection_delim);
        field_delim = field_delim.replaceAll(" ","");
        field_delim = StringEscapeUtils.unescapeJava(field_delim);
        System.out.println("collection_delim:"+collection_delim+"/field_delim:"+field_delim);
    }

    // 更新方法
    public static void update(Connection conn, int id, String name, int age) throws SQLException {
        String sql = "UPDATE users SET name=?, age=? WHERE id=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, name);
        pstmt.setInt(2, age);
        pstmt.setInt(3, id);
        int count = pstmt.executeUpdate();
        System.out.println("更新了 " + count + " 条记录");
        pstmt.close();
    }

    // 删除方法
    public static void delete(Connection conn, int id) throws SQLException {
        String sql = "DELETE FROM users WHERE id=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, id);
        int count = pstmt.executeUpdate();
        System.out.println("删除了 " + count + " 条记录");
        pstmt.close();
    }
}